<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/15/015
 * Time: 13:43
 */

namespace app\admin\controller;


use app\Lib\Api\Decoration\getHouseTypeTagList;
use app\Lib\Api\Decoration\getStyleTagList;
use app\Lib\Api\Order\addOrder;
use app\Lib\Api\Order\addOrderLog;
use app\Lib\Api\Order\choiceOrderStartTime;
use app\Lib\Api\Order\getMySite;
use app\Lib\Api\Order\getOrderList;
use app\Lib\Api\Order\getOrderPage;
use app\Lib\Api\Order\getPicFile;
use app\Lib\Api\Order\getStopWordReason;
use app\Lib\Api\Order\hangStopWordTag;
use app\Lib\Api\Order\selectSiteSupervisorUid;
use app\Lib\Api\Order\updatePicFile;
use app\Lib\Api\User\getDesignUser;
use app\Lib\Api\User\getSupervisor;
use app\Lib\TopClient;
use think\Url;

class Order extends Base
{
    public function index(){
        $keyword = input('keyword','');
        $keywordType = input('keywordType','');
        $this->assign('keyword',$keyword);
        $this->assign('keywordType',$keywordType);
        $this->assign('ajaxList',Url::build('admin/Order/ajaxList'));
        $this->assign('ajaxPage',Url::build('admin/Order/ajaxPage'));
        $this->assign('selectTime','/admin/Order/selectTime');
        $this->assign('setSupervisorUid','/admin/Order/setSupervisorUid');
        $this->assign('info','/admin/order/orderInfo');
        $this->assign('orderFile','/admin/Order/orderFile');
        return $this->fetch();
    }
    /**
     * @return array|string
     * @throws \Exception
     */
    public function ajaxPage(){
        $model = new getOrderPage();
        $post = input('post.');
        $model->setArgs('opUid',$this->login_uid);
        $model->setArgs('keyword',$post['keyword']);
        $model->setArgs('keywordType',$post['keywordType']);
        $model->setArgs('order_status',$post['order_status']);
        return (new TopClient($model))->getSuccessData();
    }


    public function ajaxList(){
        $model = new getOrderList();
        $post = input('post.');
        $model->setArgs('opUid',$this->login_uid);
        $model->setArgs('keyword',$post['keyword']);
        $model->setArgs('keywordType',$post['keywordType']);
        $model->setArgs('order_status',$post['order_status']);
        $model->setArgs('limit',$post['limit']);
        $model->setArgs('page',$post['page']);
        $data = (new TopClient($model))->getSuccessData();
        $result = ['list'=>$data];
        $this->assign('list',$result);
        return $this->fetch();
    }

    //添加工单
    public function orderInfo(){
        try {
            if ($this->request->isPost()) {
                $post = input('post.');
                $model = new addOrder();
                $model->setArgs('opUid',$this->login_uid);
                $model->setArgs('user_address',$post['user_address']);
                $model->setArgs('duration',$post['duration']);
                $model->setArgs('house_type',$post['house_type']);
                $model->setArgs('introducer_phone',$post['introducer_phone']);
                $model->setArgs('order_money',$post['order_money']);
                $model->setArgs('order_style',$post['order_style']);
                $model->setArgs('user_name',$post['user_name']);
                $model->setArgs('user_phone',$post['user_phone']);
                $model->setArgs('drawing_uid',$post['drawing_uid']);
                $model->setArgs('user_sex',$post['user_sex']);
                $model->setArgs('design_uid',$post['design_uid']);
                (new TopClient($model))->getSuccessData();
                return ['code' => 1, 'msg' => '操作成功'];
            } else {
                //获取户型
                $houseType = (new TopClient(new getHouseTypeTagList()))->getSuccessData();
                //获取风格
                $style = (new TopClient(new getStyleTagList()))->getSuccessData();
                //获取设计师
                $user = (new TopClient(new getDesignUser()))->getSuccessData();
                //获取画图人员
                $this->assign('houseType',$houseType);
                $this->assign('user',$user);
                $this->assign('style',$style);
                $this->assign('addUrl', Url::build('admin/Order/orderInfo'));
                return $this->fetch();
            }
        }catch (\Exception $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }
    }

    //选择开工时间
    public function selectTime(){
        try {
            if ($this->request->isPost()) {
                $post = input('post.');
                $model =  new choiceOrderStartTime();
                $model->setArgs('order_number',$post['id']);
                $model->setArgs('opUid',$this->login_uid);
                $model->setArgs('orderStartTime',strtotime($post['orderStartTime']));
                (new TopClient($model))->getSuccessData();
                return ['code' => 1, 'msg' => '操作成功'];
            } else {
                $this->assign('order_id',input('menu_id'));
                $this->assign('addUrl', Url::build('admin/Order/selectTime'));
                $this->assign('order_number', input('menu_id'));
                return $this->fetch();
            }
        }catch (\Exception $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }
    }
    //设置监工
    public function setSupervisorUid(){
        try {
            if ($this->request->isPost()) {
                $post = input('post.');
                $model =  new selectSiteSupervisorUid();
                $model->setArgs('opUid',$this->login_uid);
                $model->setArgs('order_number',$post['order_number']);
                $model->setArgs('supervisorUid',$post['design_uid']);
                $model->setArgs('reason','');
                (new TopClient($model))->getSuccessData();
                return ['code' => 1, 'msg' => '操作成功'];
            } else {
                //获取设计师
                $user = (new TopClient(new getSupervisor()))->getSuccessData();
                //获取画图人员
                $this->assign('user',$user);
                $this->assign('order_number',input('menu_id'));
                $this->assign('addUrl', Url::build('admin/Order/setSupervisorUid'));
                return $this->fetch();
            }
        }catch (\Exception $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }
    }

    //工单回复
    public function orderReply(){
        try {
            if ($this->request->isPost()) {
                $post = input('post.');
                if(empty($post['mySite'])){
                    return ['code'=>-1,'msg'=>'请选择要回复的工地'];
                }
                if(!empty($post['dayWord']) || isset($post['img_id']) ){
                    $model =  new addOrderLog();
                    $model->setArgs('order_number',$post['mySite']);
                    $model->setArgs('content',$post['dayWord']);
                    $model->setArgs('opUid',$this->login_uid);
                    $file['img']  = [];
                    if(isset($post['img_id'])){
                        foreach ($post['img_id'] as $k=>$v){
                            $file['img'][$k]['time_id'] = 0;
                            $file['img'][$k]['img_id'] = $v;
                        }
                    }
                    $model->setArgs('file',$file);
                    (new TopClient($model))->getSuccessData();
                }

                if(!empty($post['stop']) || !empty($post['stopReason']) ){
                    $model =  (new hangStopWordTag());
                    $model->setArgs('tagId',$post['stop']);
                    $model->setArgs('opUid',$this->login_uid);
                    $model->setArgs('content',$post['stopReason']);
                    $model->setArgs('order_number',$post['mySite']);
                    (new TopClient($model))->getSuccessData();
                }
                return ['code'=>1,'msg'=>'操作成功'];
            } else {
                //获取工地
                $mySite = (new getMySite());
                $mySite->setArgs('opUid',$this->login_uid);
                $result= (new TopClient($mySite))->getSuccessData();
                $stop = new getStopWordReason();
                $stopReason= (new TopClient($stop))->getSuccessData();
                $this->assign('mySite',$result);
                $this->assign('stop',$stopReason);
                $this->assign('addUrl', Url::build('admin/Order/orderReply'));
                return $this->fetch();
            }
        }catch (\Exception $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }
    }


    public function orderFile(){
        try {
            if ($this->request->isPost()) {
                $post  = input('post.');
                $model =  new updatePicFile();
                if(
                    (isset( $post['new_img_id']) && !empty($post['new_img_id']))
                ){
                    $model->setArgs('new_img_id',$post['new_img_id']);

                }
                if(isset($post['del_img_id']) &&  !empty($post['del_img_id'])){
                    $model->setArgs('del_img_id',$post['del_img_id']);
                }
                $url = [];
                if($post['url']  && !empty($post['url'])){
                    foreach ($post['url'] as $key => $v){
                        $url['id'] = $key;
                        $url['url'] = $v;
                    }
                    $model->setArgs('url',$url);
                }
                $model->setArgs('opUser',$this->login_uid);
                $model->setArgs('order_number',$post['order_number']);
                (new TopClient($model))->getSuccessData();
                return ['code'=>1,'msg'=>'操作成功'];
            } else {
                $model =  (new getPicFile());
                $model->setArgs('order_number',input('menu_id'));
                $data = (new TopClient($model))->getSuccessData();
                if(!isset($data['pathUrl']) || empty($data['pathUrl']) ){
                    $data['pathUrl']=[
                        0=>''
                    ] ;
                }
                $this->assign('data',$data);
                $this->assign('addUrl', Url::build('admin/Order/orderFile'));
                $this->assign('order_number', input('menu_id'));
                return $this->fetch();
            }
        }catch (\Exception $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }
    }
}